//
//  DrivesScrollView.m
//  carcareIOS
//
//  Created by tianxing on 16/3/8.
//  Copyright © 2016年 baozun. All rights reserved.
//

#import "DrivesCell.h"

#import "DrivesMgr.h"
#import "NewOneWayCell.h"
#import "DrivesScoreView.h"
#import "RadarPictureViewController.h"
#import "NetDrives.h"
#import "DrivesModel.h"

#define SCORE_HEIGHT 151
#define GAP 0
//#define SCORE_HEIGHT_MIN 58

@interface DrivesCell()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate,UIScrollViewDelegate>

@property (nonatomic, strong) DrivesMgr *drivesMgr;          // 数据管理类

@property (nonatomic, strong) DrivesScoreView *scoreView;

@property (nonatomic, strong) NoTripDataView *tripView;      // 无行程

@end

@implementation DrivesCell

- (void)dealloc{
    [self.tableView removeObserver:self forKeyPath:@"contentOffset"];
    [[NSNotificationCenter defaultCenter] removeObserver:_tableView];
}
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUpView];
    }
    return self;
}

- (void)setUpView{
    
    [[NSNotificationCenter defaultCenter] addObserver:self.tableView selector:@selector(reloadData) name:DRIVE_PLACE_UPDATE object:nil];
    
    [self.tableView addObserver:self
                      forKeyPath:@"contentOffset"
                         options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
                         context:NULL];
    [self addSubview:self.tableView];
    
}

-(void)updateTableViewContentInset{
    NSInteger count = self.drivesModel.travelModels.count;
    UIEdgeInsets edge = self.tableView.contentInset;
    edge.bottom = 0;
    if (count > 0) {
        CGFloat height = SCORE_HEIGHT + GAP + [NewOneWayCell cellHeight] * count;
        CGFloat sub = self.tableView.height - height;
        if (sub > 0) {
            edge.bottom = sub;
        }
    }
    self.tableView.contentInset = edge;
}

#pragma mark --- tabeleView delegate

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.drivesModel.travelModels.count == 0) {
        return tableView.height;
    }
    if (indexPath.row == 0) {
        return SCORE_HEIGHT + GAP;
    }
    return  [NewOneWayCell cellHeight];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // 根据模型类的数据
    return self.drivesModel.travelModels.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *cellID = @"cell";
    
    if (self.drivesModel.travelModels.count == 0) {
        cellID = @"cellno";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            [cell addSubview:self.tripView];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = COLOR_HEXSTRING(@"#F6F6F9");

        [self.tripView changeContentText:self.noDrivesTips];
        return cell;
    }
    
    if (indexPath.row == 0) {
        cellID = @"cell0";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            [cell addSubview:self.scoreView];
            cell.backgroundColor = COLOR_HEXSTRING(@"#F6F6F9");
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        self.scoreView.DetailtitleText = [NSString stringWithFormat:@"总里程%.01fkm \t | \t 总油费%.02f元",self.drivesModel.drivingMiles,self.drivesModel.dayFuelCost];
        self.scoreView.scoreText = STRING_INT(self.drivesModel.drivingMark);
        return cell;
    }
    
    NewOneWayCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[NewOneWayCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    OneDriveModel *oneDriveModel = self.drivesModel.travelModels[indexPath.row - 1];
    
    cell.model = oneDriveModel;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row > 0) {
        if (self.delegate) {
            [[StatisticsMgr sharedInstance] addClickID:@"行程-点击查看行程的详细记录" description:[tableView cellForRowAtIndexPath:indexPath].description];
            DRIVERSMGR.selelctDrivesModel = self.drivesModel;
            [self.delegate drivesDidClickSelectModelIndex:indexPath.row - 1];
        }
    }
}

#pragma mark - scroll

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    if (self.delegate) {
        if (velocity.y == 0) {
            [self.delegate drivesDidScrollEndTop:scrollView.contentOffset.y];
        }
    }
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (self.delegate) {
        [self.delegate drivesDidScrollEndTop:scrollView.contentOffset.y];
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (self.delegate) {
        [self.delegate drivesDidScrollEndTop:scrollView.contentOffset.y];
    }
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    if (self.delegate) {
        [self.delegate drivesDidScrollEndTop:scrollView.contentOffset.y];
    }
}

#pragma mark - set get

- (UITableView *)tableView{
    if (_tableView == nil) {
        _tableView  = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, __MAIN_HEIGHT_64_TABBAR - 69) style:UITableViewStylePlain]; // frame 大小具体来定
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;

    }
    return _tableView;
}

-(DrivesScoreView *)scoreView{
    if (!_scoreView) {
        _scoreView = [[DrivesScoreView alloc] initWithFrame:CGRectMake(0, GAP, __MAIN_WIDTH, SCORE_HEIGHT)];
    }
    return _scoreView;
}

-(DrivesMgr *)drivesMgr{
    if (!_drivesMgr) {
        _drivesMgr = [[DrivesMgr alloc] init];
    }
    return _drivesMgr;
}

-(NoTripDataView *)tripView{
    if (!_tripView) {
        _tripView = [[NoTripDataView alloc] initWithFrame:self.tableView.bounds];
        _tripView.top += GAP;
        _tripView.height -= GAP;
    }
    return _tripView;
}

- (void)setDrivesModel:(DrivesModel *)drivesModel{
    _drivesModel  = drivesModel;
    [self.tableView reloadData];
}


#pragma mark - kvc
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if (object == self.tableView) {
        if ([change[NSKeyValueChangeNewKey] isEqual:change[NSKeyValueChangeOldKey]]) {
            return;
        }
        if ([keyPath isEqualToString:@"contentOffset"]){
            if (self.delegate) {
                [self.delegate drivesDidScrollChangeTop:self.tableView.contentOffset.y];
            }
        }
    }
}
@end
